package dept

import (
	"context"
	"go-zero-project/services/user/rpc/pb"

	"go-zero-project/services/user/api/internal/svc"
	"go-zero-project/services/user/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewDeptUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateLogic) DeptUpdate(req *types.DeptUpdateReq) error {
	// 1.builder rpc request
	updateReq := pb.DeptUpdateReq{
		DeptId:   req.DeptId,
		Name:     req.Name,
		ParentId: req.ParentId,
		OrderNum: int32(req.OrderNum),
	}
	// 2.request rpc
	_, err := l.svcCtx.UserCenterClient.DeptUpdate(l.ctx, &updateReq)
	if err != nil {
		return err
	}
	// 3.return obj
	return nil
}
